package com.weixin.paleituo.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.eventbus.VersionEvent;
import com.weixin.paleituo.util.LogUtils;

import org.greenrobot.eventbus.EventBus;


public class VersionUpdatePopuwindow extends PopupWindow {
	private String TAG ="VersionUpdatePopuwindow";


	private TextView update,not_update;
	private LinearLayout pro_ll;
	private LinearLayout update_ll;
	private LinearLayout noupdate_ll;
	private TextView pro_text,confir_text,load_text;
	private View view;
	private AppVersionUpdate.OnClick onclick;
	private ProgressBar update_pro;
	private int isForce;
	public void setOnClick(AppVersionUpdate.OnClick onclick){
		this.onclick=onclick;
	}
	public VersionUpdatePopuwindow(Context context, boolean isFirst , int isForce){
		this.isForce=isForce;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view=inflater.from(context).inflate(R.layout.version_update_layout, null);
		update=(TextView) view.findViewById(R.id.update_text);
		not_update=(TextView) view.findViewById(R.id.not_update_text);
		pro_ll=(LinearLayout) view.findViewById(R.id.pro_ll);
		update_ll=(LinearLayout) view.findViewById(R.id.update_ll);
		noupdate_ll=(LinearLayout) view.findViewById(R.id.noupdate_ll);
		pro_text=(TextView) view.findViewById(R.id.pro_text);
		confir_text=(TextView) view.findViewById(R.id.confir_text);
		update_pro=(ProgressBar) view.findViewById(R.id.update_pro);
		load_text=(TextView) view.findViewById(R.id.load_text);
		if(isForce==1){
			update_ll.setVisibility(View.GONE);
			pro_ll.setVisibility(View.VISIBLE);
			load_text.setText("重要版本强制升级中");
		}

		if(!isFirst){//从设置处点击
			update_ll.setVisibility(View.GONE);
			noupdate_ll.setVisibility(View.VISIBLE);
		}
		update.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setFocusable(false);
				update_ll.setVisibility(View.GONE);
				pro_ll.setVisibility(View.VISIBLE);
				onclick.onClick();
			}
		});
		not_update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
				//不更新
				VersionEvent versionEvent =new VersionEvent();
				versionEvent.setType(1);
				EventBus.getDefault().post(versionEvent);


			}
		});
		confir_text.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		this.setContentView(view);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		this.setOutsideTouchable(false);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
	}
	//更新进度
	public void  setProgress(int progress){
		pro_text.setText(progress+"%");
		update_pro.setProgress(progress);
		LogUtils.i(TAG,"progress  ::"+progress);
	}
}
